package com.lsa.letcode.the129.sumRootToLeaf;

import com.lsa.letcode.common.TreeNode;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int sumNumbers(TreeNode root) {
    	if (root == null){
    		return 0;
    	}
        return sumNumbers(root, 0);
    }
    
    public int sumNumbers(TreeNode root, int parent) {
    	if (root == null){
    		return 0;
    	} else {
			final int parentSum = 10 * parent + root.val;
			if (root.left == null && root.right == null){
				return parentSum;
			}
			else {
				return sumNumbers(root.left, parentSum ) + sumNumbers(root.right, parentSum );
			}
		}
    }
}